package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.pojo.*;
import com.example.demo.service.*;

import com.example.demo.pojo.IntegralCommodityList;
import com.example.demo.service.IntegralCommodityListService;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/integralCommodityList")
@Api(value = "积分管理",tags = "积分管理")
public class IntegralCommodityListController {
    @Resource
    private IntegralCommodityListService integralCommodityListService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderFormService orderFormService;
    @Autowired
    private CommodityListDao commodityListDao;
    @Autowired
    private OrderChildFormService orderChildFormService;

    @RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.POST)

    @ResponseBody
    @ApiOperation(value = "删除积分商品", notes = "删除积分商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "integralgoodsid", value = "积分商品id")})
    public String deleteByPrimaryKey(Integer integralgoodsid) {
        int res = integralCommodityListService.deleteByPrimaryKey(integralgoodsid);
        return JSON.toJSONString(res);
    }

    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "添加积分商品", notes = "添加积分商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "commodityScorecommodityscore", value = "商品积分"),
            @ApiImplicitParam(name = "tradename", value = "商品名称")
    })
    public String insert(IntegralCommodityList record) {
        int res = integralCommodityListService.insert(record);
        return JSON.toJSONString(res);
    }

    @RequestMapping(value = "/udateByPrimaryKey", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改积分商品", notes = "修改积分商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "commodityScorecommodityscore", value = "商品积分"),
            @ApiImplicitParam(name = "tradename", value = "商品名称")
    })
    public String updateByPrimaryKey(IntegralCommodityList record) {
        int res = integralCommodityListService.updateByPrimaryKey(record);
        return JSON.toJSONString(res);
    }

    @ResponseBody
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有积分商品", notes = "查询所有积分商品")
    public String getAll() {
        List<IntegralCommodityList> list = integralCommodityListService.getAll();
        List<CommodityList> lists = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CommodityList c = commodityListDao.selectByPrimaryKey(list.get(i).getIntegralgoodsid());
            lists.add(c);
        }
        return JSON.toJSONString(lists);
    }

    //返回商品详情
    @RequestMapping(value = "/jifen", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "积分兑换", notes = "积分兑换")
    @ApiImplicitParams({@ApiImplicitParam(name = "addressId", value = "地址id"),
            @ApiImplicitParam(name = "integralgoodsid", value = "商品id")
    })
    public String selectById(int integralgoodsid, HttpSession httpSession, int addressId) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return "请先登录";
        } else {
            IntegralCommodityList integralCommodityList = integralCommodityListService.selectByPrimaryKey(integralgoodsid);
            int jifen = integralCommodityList.getCommodityScorecommodityscore();
            if (user.getIntegral() < jifen) {
                return "你的积分不足";
            } else {
                //减掉用户积分
                user.setIntegral(user.getIntegral() - jifen);
                userService.updateByUserId(user);
                OrderForm orderForm = new OrderForm();
                orderForm.setAddressid(addressId);
                orderForm.setUserid(user.getUserid());
                Long number = System.currentTimeMillis();
                orderForm.setOrdernumber(number.toString());
                //添加进用户表
                orderFormService.insert(orderForm);
                OrderChildForm orderChildForm = new OrderChildForm();
                orderChildForm.setGoodsid(integralgoodsid);
                orderChildForm.setOrderid(orderForm.getOrderid());
                orderChildFormService.insert(orderChildForm);
                return "兑换成功，商品将在1到2个工作日内送达";
            }
        }
    }
}
